package com.xiaohudie.controller;

import com.xiaohudie.mapper.MiaoShaMapper;
import com.xiaohudie.pojo.AjaxResult;
import com.xiaohudie.pojo.YouHuiQuan;
import com.xiaohudie.servier.Impi.MiaoShaServicerImpl;
import com.xiaohudie.util.RedisIdWork;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.*;

/**
 * @Author xiao hu die
 * @Date 2022/3/6 14:57
 * @Version 1.0
 */
@RestController
@RequestMapping("/miaosha")
public class MiaoShaController {
    @Resource
    private RedisIdWork redisIdWork;
    @Resource
    private MiaoShaServicerImpl miaoShaServicer;
    @GetMapping("/test")
    public void test() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(100);
        ExecutorService executorService = new ThreadPoolExecutor(
                /**主线程数量*/
                2,
                /**最大线程数量*/
                5,
                /**线程等待时间*/
                2L,
                /**等待时间的单位时分秒*/
                TimeUnit.SECONDS,
                /**新建阻塞队列*/
                new ArrayBlockingQueue<>(3),
                /**线程工厂*/
                Executors.defaultThreadFactory(),
                /**拒绝策略,JDK默认4个*/
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
        long begin = System.currentTimeMillis();
        executorService.execute(()->{
            for (int i = 0; i <100 ; i++) {
                long chengweiyi = redisIdWork.nextId("chengweiyi");
                countDownLatch.countDown();
                System.out.println(chengweiyi);
            }
        });
       countDownLatch.await();
        long endTime = System.currentTimeMillis();
        System.out.println("time:"+(endTime-begin));
   }
  @GetMapping("/buy")
    public AjaxResult buy(){
        Integer id =1;
      AjaxResult ajaxResult = miaoShaServicer.queryById(id);
      return ajaxResult;

  }
}
